/*
 * 
 */

package com.memorytech.starchive.xmlparser;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.memorytech.starchive.entity.BarcodeInfo;
import com.memorytech.starchive.entity.FileInformation;
import com.memorytech.starchive.entity.Medium;

import android.util.Log;

/**
 * The Class BarcodeParser.
 * 
 * @author Tam-LT
 */
public class BarcodeParser extends DefaultHandler {
    private ArrayList<FileInformation> fileInformations;
    private FileInformation fileInformation;
    private Medium medium;
    private BarcodeInfo barcodeInfo;

    /**
     * Returns the data object.
     * 
     * @return the data
     */
    public BarcodeInfo getData() {
        return barcodeInfo;
    }

    /**
     * This gets called when the xml document is first opened
     * 
     * @throws SAXException
     */
    @Override
    public void startDocument() throws SAXException {
        barcodeInfo = new BarcodeInfo();
    }

    /**
     * Called when it's finished handling the document
     * 
     * @throws SAXException
     */
    @Override
    public void endDocument() throws SAXException {

    }

    /**
     * This gets called at the start of an element. Here we're also setting the
     * booleans to true if it's at that specific tag. (so we know where we are)
     * 
     * @param namespaceURI
     * @param localName
     * @param qName
     * @param atts
     * @throws SAXException
     */
    @Override
    public void startElement(String namespaceURI, String localName, String qName,
            Attributes atts) throws SAXException {

        if (localName.equals("FileInformation")) {
            fileInformations = new ArrayList<FileInformation>();
        }
        else if (localName.equals("File")) {
            fileInformation = new FileInformation();
            fileInformation.setName(atts.getValue("Name"));
            fileInformation.setSize(Integer.parseInt(atts.getValue("Size")));
            fileInformation.setPreviewFile(atts.getValue("PreviewFile"));
            fileInformation.setImageFile(atts.getValue("ImageFile"));
            fileInformation.setFileStatus(atts.getValue("FileStatus"));
            fileInformation.setCreateTime(atts.getValue("CreateTime"));
            fileInformations.add(fileInformation);

        } else if (localName.equals("Request")) {
            barcodeInfo.setBarCode(atts.getValue("Barcode"));

        } else if (localName.equals("NumberOfInformation")) {
            barcodeInfo.setTape(Integer.parseInt(atts.getValue("Tape")));
            barcodeInfo.setFile(Integer.parseInt(atts.getValue("File")));

        } else if (localName.equals("Medium")) {
            medium = new Medium();
            medium.setIdentify(atts.getValue("Identify"));
            medium.setFreeSpace(atts.getValue("FreeSpace"));
            medium.setTotalSpace(atts.getValue("TotalSpace"));

            barcodeInfo.setMedium(medium);

        }
    }

    /**
     * Called at the end of the element. Setting the booleans to false, so we
     * know that we've just left that tag.
     * 
     * @param namespaceURI
     * @param localName
     * @param qName
     * @throws SAXException
     */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException {
        Log.v("endElement", localName);
        if (localName.equals("FileInformation")) {
            barcodeInfo.setFileInformations(fileInformations);
        }
    }

    /**
     * Calling when we're within an element. Here we're checking to see if there
     * is any content in the tags that we're interested in and populating it in
     * the Config object.
     * 
     * @param ch
     * @param start
     * @param length
     */
    @Override
    public void characters(char ch[], int start, int length) {
    }
}
